//
//  AnimationView.m
//  项目二
//
//  Created by rimi1 on 2016/11/17.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "AnimationView.h"
#import "AnimationData.h"

static AnimationView *_animationView = nil;

@interface AnimationView ()

@property (nonatomic, strong) AnimationData *data;// 基本数值
@property (nonatomic, strong) UIView *maskView;// 遮罩层
@property (nonatomic, strong) UIView *animationView;// 白色动画视图
@property (nonatomic, strong) UIView *centerView;// 圆圈中心点视图
@property (nonatomic, strong) CABasicAnimation *basicAnimation;// 动画
@property (nonatomic, strong) CAShapeLayer *shapeLayer;// 弧形layer
@property (nonatomic, strong) CAShapeLayer *maskLayer;// 遮罩layer

@end

@implementation AnimationView

+ (instancetype)sharedAnimation {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _animationView = [[AnimationView alloc] init];
    });
    return _animationView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.maskView];
        [self addSubview:self.animationView];
        [self.animationView addSubview:self.centerView];
        [self.centerView.layer addSublayer:self.shapeLayer];
        [self.centerView.layer addSublayer:self.maskLayer];
    }
    return self;
}
// 出现动画
- (void)showAnimationWithTitle:(NSString *)title {
    // 添加到根视图
    UIWindow *window = [UIApplication sharedApplication].windows[0];
    [window addSubview:self];
    // 创建文字视图
    [self initiaAnimationViewWithTitle:title];
    // 视图动画
    [UIView animateWithDuration:self.data.viewDuration animations:^{
        self.maskView.alpha = 0.2;
    }];
    // 添加basic动画
    [self.shapeLayer addAnimation:self.basicAnimation forKey:nil];
}
// 隐藏动画
- (void)hideAnimation {
    [UIView animateWithDuration:self.data.viewDuration animations:^{
        self.maskView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        // 删除文字视图
        [[self viewWithTag:200] removeFromSuperview];
    }];
}

// 初始化动画视图，并返回让让弧形围着转的视图
- (void)initiaAnimationViewWithTitle:(NSString *)title {
    [self noTextStatus];
    // 文字存在 且 不为@"" 则创建
    if (title && ![title isEqualToString:@""]) {
        // 文字宽加间距
        CGFloat labelWidth = [AnimationData widthWithText:title] + 20;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.animationView.bounds.size.height, labelWidth, 17)];
        label.font = [UIFont boldSystemFontOfSize:17];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = title;
        label.tag = 200;
        
        [self.animationView addSubview:label];
        
        // 加入label，重新布局
        CGRect rect = self.animationView.bounds;
        rect.size.height = 130;
        // 当大于，则等于labelWidth
        if (labelWidth > self.animationView.bounds.size.width ) {
            rect.size.width = labelWidth;
        }
        self.animationView.bounds = rect;
        
        CGPoint labelPoint = label.center;
        labelPoint.x = self.animationView.bounds.size.width/2;
        label.center = labelPoint;
        
        CGPoint point = self.centerView.center;
        point.x = self.animationView.bounds.size.width/2;
        self.centerView.center = point;
    }
    
}
// 没有文本的视图bounds
- (void)noTextStatus {
    self.animationView.bounds = CGRectMake(0, 0, self.data.AWidth, self.data.AHeight);
}

#pragma mark - get

- (AnimationData *)data {
    if (!_data) {
        _data = [[AnimationData alloc] init];
    }
    return _data;
}

- (UIView *)maskView {
    
    if (!_maskView) {
        
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.data.width, self.data.height)];
        _maskView.backgroundColor = [UIColor whiteColor];
        _maskView.alpha = 0;
        
    }
    return _maskView;
}

- (UIView *)animationView {
    
    if (!_animationView) {
        
        _animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.data.AWidth, self.data.AHeight)];
        _animationView.layer.cornerRadius = 10;
        _animationView.layer.shadowOffset = CGSizeMake(7, 7);
        _animationView.layer.shadowColor = [UIColor blackColor].CGColor;
        _animationView.layer.shadowOpacity = 0.3;
        _animationView.backgroundColor = [UIColor whiteColor];
        _animationView.center = self.maskView.center;
        
    }
    return _animationView;
}

- (UIView *)centerView {
    
    if (!_centerView) {
        // 中来做中心点视图(x轴随父视图居中，y轴固定)
        _centerView = [[UIView alloc] initWithFrame:CGRectMake(self.animationView.bounds.size.width/2, 50, 0, 0)];
        
    }
    return _centerView;
}

- (CABasicAnimation *)basicAnimation {
    
    if (!_basicAnimation) {
        
        _basicAnimation = [CABasicAnimation animation];
        _basicAnimation.keyPath = @"transform.rotation.z";
        // 默认中心点为父视图左上角
        _basicAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
        _basicAnimation.duration = self.data.arcDuration;
        _basicAnimation.repeatCount = CGFLOAT_MAX;
        
    }
    return _basicAnimation;
}

- (CAShapeLayer *)shapeLayer {
    
    if (!_shapeLayer) {
        // 默认起点是3点钟，旋转点是视图左上角，M_PI == 180° 剩下以此类推
        UIBezierPath *bezier = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:self.data.radius startAngle:0 endAngle:M_PI_2 clockwise:YES];
        
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer.strokeColor = [UIColor orangeColor].CGColor;
        _shapeLayer.lineWidth = 5;
        _shapeLayer.path = bezier.CGPath;
        
    }
    return _shapeLayer;
}

- (CAShapeLayer *)maskLayer {
    
    if (!_maskLayer) {
        
        _maskLayer = [CAShapeLayer layer];
        _maskLayer.fillColor = [UIColor clearColor].CGColor;
        _maskLayer.strokeColor = [UIColor whiteColor].CGColor;
        // 比转圈的线条稍宽
        _maskLayer.lineWidth = 6;
        _maskLayer.path = self.shapeLayer.path;
        
    }
    return _maskLayer;
}

@end















